package com.su.config;


import com.su.common.JacksonObjectMapper;
import com.su.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer{
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration empRegistration = registry.addInterceptor(loginInterceptor);
        //白名单url
        List<String> urlList = new ArrayList();
        urlList.add("/employee/login");
        urlList.add("/employee/logout");
        urlList.add("/backend/**");
        urlList.add("/common/**");
        urlList.add("/front/**");
        urlList.add("/user/login");
        urlList.add("/user/loginout");
        urlList.add("/user/sendMsg");
        //拦截所有
        empRegistration.addPathPatterns("/**");
        //放行白名单
        empRegistration.excludePathPatterns(urlList );
    }

    /**
     * mvc消息转换器器
     * @param converters
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //创建消息转换器对象
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //设置对象转换器
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将消息转换器主驾到mvc转换器集合中
        converters.add(0,messageConverter);
    }
}

